<template>
	<view class="index">
		<view class="content">
			<view class="input-list">
				<!-- input输入框 -->
				<input class="input-item" v-if="focus" adjust-position="false" auto-blur="true" @blur="inputCodeBlur"
					@input="inputCode" :focus="focus" v-model="code" type="text" oneTimeCode
					maxlength="5" />
				<!-- 验证码输入框 -->
				<view class="code-list" @click="focusClick">
					<view class="code-item" v-for="(item,index) in 5" :key="index"
						:style="(index == code.length && focus ? 'border-color:#F5BF46;':'')">{{code[index]}}</view>
				</view>
			</view>
			<view class="teacher-input" @click="toTeacherInput">
				<text>教师入口</text>
			</view>
		</view>
		<button class="tz" @click="tz" style="background-color:#F5BF46;">下一步</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '', // 验证码字段
				focus: false, // input是否聚焦
				kg: null,
				kgId: null
			}
		},
		watch: {
			// 监听倒计时
			'codeBtn.codeNumber': {
				handler(val) {
					// 这里监听用户输入完完整的验证码，去调接口验证。
					if (val == 0) {
						this.codeBtn.codeName = '重新获取'
						clearInterval(this.timer)
					}
				}
			}
		},
		
		methods: {
			tz(){ //跳转
				//把code给后台数据库对比
				uni.$api.kgFindByCode(this.code).then(result =>{
					if(result == null){
						uni.showToast({
							title: '编码输入有误',
							icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
							duration: 2000    //持续时间为 2秒
						})
					}else{
						this.kgId = result.id
						uni.setStorageSync("kgId",this.kgId)//把幼儿园id存本地
						
						this.toKin(); //调用跳转方法去选择身份页面
					}
				})
			},
			toKin(){
				uni.$common.navigateTo("/subpkg/kin/kin")
			},
			toTeacherInput(){
				uni.$common.navigateTo("/pages/input/teacherInput/teacherInput")
			},
			// 点击聚焦输入框
			focusClick() {
				this.focus = true
			},
			// 输入框失去焦点
			inputCodeBlur(e) {
				let value = e.detail.value
				this.focus = false
			},
			// 输入框内容变化事件
			inputCode(e) {
				let value = e.detail.value
				this.code = value
			},
		}
	}
</script>
<style lang="scss" scoped>
	.index {
		background-color: #fff;
		position: fixed;
		top:0;
		left: 0%;
		width: 100%;
		height: 100%;
		object-fit: cover;

		.content {
			padding: 30rpx 60rpx;

			.input-list {
				margin-top:150rpx ;
				display: flex;
				align-items: center;

				.input-item {
					width: 0rpx;
				}

				.code-list {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.code-item {
						width: 80rpx;
						height: 80rpx;
						text-align: center;
						line-height: 80rpx;
						border: 1px solid #AEB5B9;
						border-radius: 10rpx;
					}
				}
			}
			
			.teacher-input{
				position: absolute;
				right: 50rpx;
				top: 50rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 50rpx;
				border: 1px solid #AEB5B9;
				padding: 0 50rpx;
				font-size: 20rpx;
				
			}
			.teacher-input text{
				font-size: 0.8rem;
				color: #9d9d9d;
			}
		}
		.tz{
			position: absolute;
			bottom: 70rpx;
			left: 50%;
			transform: translate(-50%, 0);
			width: 300rpx;
			height: 80rpx;
			color: #FFFFFF;
			font-size: 30rpx;
			font-weight: bold;
			border-radius: 45rpx;
		}
	}
</style>
